package com.heima.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import com.heima.wemedia.client.WemediaFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;

@RestController
@Api(tags = "频道管理", description = "频道管理API")
public class ChannelController {

    @Autowired
    private AdChannelService channelService;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据名称分页查询频道列表")
    @ApiImplicitParam(name = "dto", value = "频道请求对象", required = true, dataType = "ChannelDto")
    @PostMapping("/api/v1/channel/list")
    public PageResult<ChannelDto> findByPage(@RequestBody ChannelDto dto) {
        return channelService.findByPage(dto);
    }


    @PostMapping("/api/v1/channel/save")
    public ResponseResult save(@RequestBody AdChannel channel) {
//        查询频道名字是否存在

        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdChannel::getName, channel.getName());

        int count = channelService.count(queryWrapper);
        if (count != 0) {
            throw new LeadException(AppHttpCodeEnum.CHANNEL_NAME_EXIST);
        }

        channelService.save(channel);
        return ResponseResult.ok();
    }

    @PutMapping("/api/v1/channel/update")
    public ResponseResult update(@RequestBody AdChannel channel) {

        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(AdChannel::getName, channel.getName())
                .ne(AdChannel::getId, channel.getId());

        int count = channelService.count(queryWrapper);
        if (count != 0) {
            throw new LeadException(AppHttpCodeEnum.CHANNEL_NAME_EXIST);
        }

        channelService.updateById(channel);

        return ResponseResult.ok();
    }

    @DeleteMapping("/api/v1/channel/del/{id}")
    public ResponseResult del(@PathVariable(name = "id") Integer id) {

        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(AdChannel::getId, id)
                .eq(AdChannel::getStatus, false);

        int count = channelService.count(queryWrapper);
        if (count == 0) {
            throw new LeadException(AppHttpCodeEnum.DATA_CAN_NOT_DEL);
        }

        channelService.removeById(id);

        return ResponseResult.ok();

    }

    @GetMapping("/api/v1/channel/channels")
    public ResponseResult<AdChannel> channels() {
        return ResponseResult.ok(channelService.list());
    }

    @Autowired
    private WemediaFeign feign;

}
